//
//  M1User+M1.m
//  NewMarryU
//
//  Created by apple on 2018/7/23.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1User+M1.h"
#import "M1LoginVC.h"
#import "M1RealNameViewController.h"
#import "M1PersonalDataVC.h"
#import "M1PhotoGalleryVC.h"
@implementation M1User (M1)
+(void)save:(NSDictionary *)dic{
    [M1SaveTool m1_saveUserConfig:dic];
}

+(void)saveModel:(M1User *)user{
    NSDictionary *dic = [user mj_keyValues];
    [M1SaveTool m1_saveUserConfig:dic];
}

+(void)remove{
    [M1SaveTool m1_removeUser];
}

+(void)m1_userPermissionForm:(UIViewController *)vc type:(M1UserPermission)type to_uid:(NSString *)to_uid permission:(void(^)(NSDictionary *resDic))block{
    if (![M1User user]) {
        [M1Alert m1_alertWithTitle:nil message:m1_loginText buttonTitles:@[@"取消",@"登录"] action:^(NSInteger index) {
            if (index == 1) {
                M1LoginVC *m1_LoginVC = [M1LoginVC new];
                m1_LoginVC.m1_changeBackImage = YES;
                m1_LoginVC.m1_changeNavTitleColor = YES;
                [vc.navigationController pushViewController:m1_LoginVC animated:YES];
            }
        }];
        
        return;
    }

    NSString *power_type;
    switch (type) {
        case M1UserPermission_Chat:
            power_type = @"chat";
            break;
        case M1UserPermission_Search:
            power_type = @"search";
            break;
        case M1UserPermission_ToChat:
            power_type = @"to_chat";
            break;
            
        default:
            break;
    }
    
    if (power_type.length == 0) {
        [M1Alert m1_alertWithTitle:nil message:@"权限请求类型错误"];
        return;
    }
    
    NSDictionary *parmas= @{@"power_type":power_type};
    if (to_uid.length > 0) {
        parmas= @{@"power_type":power_type,
                  @"to_uid":to_uid
                  };
    }
    [M1HttpNet requestWithMethod:m1_power_url params:parmas success:^(id resDic, NSInteger code, NSString *message) {
        if (block) {
            block(resDic);
        }
    } fail:^(id resDic, NSInteger code, NSString *message) {
        if (code >-20000 &&code<=-10000) {
            //会员
            [M1Alert m1_alertWithTitle:nil message:message buttonTitles:@[@"取消", @"前往"] action:^(NSInteger index) {
                if (index == 1) {
                    //会员
                    M1VIPCenterVC *m1_vipVC = [[M1VIPCenterVC alloc]init];
                    m1_vipVC.hidesBottomBarWhenPushed = YES;
                    [vc.navigationController pushViewController:m1_vipVC animated:YES];
                }
            }];
        }else if (code >=-20003 &&code<=-20000){
            //
            if([M1User user].basic.photos.count == 0){
                message = @"您的相册未上传照片，是否上传？";
            }
            if([[M1User user].basic.avatar.img.url length] <= 0){
                message = @"您的头像未上传，是否上传？";
            }
            [M1Alert m1_alertWithTitle:nil message:message buttonTitles:@[@"取消",@"确定"] action:^(NSInteger index) {
                if (index == 1) {
                    if ([M1User user].basic.photos.count == 0||[[M1User user].basic.avatar.img.url length] <= 0) {
                        M1PhotoGalleryVC *m1_phtoVC = [M1PhotoGalleryVC new];
                        m1_phtoVC.hidesBottomBarWhenPushed = YES;
                        [vc.navigationController pushViewController:m1_phtoVC animated:YES];
                    }else{
                        M1PersonalDataVC *m1_psonVC = [M1PersonalDataVC new];
                        m1_psonVC.hidesBottomBarWhenPushed = YES;
                        [vc.navigationController pushViewController:m1_psonVC animated:YES];
                    }
                }
            }];
        }else{
            [M1Alert m1_alertWithTitle:nil message:message];
        }
    } showAni:YES];
}
-(void)m1_joinblocklist:(NSString *)to_uid{
    [M1HttpNet requestWithMethod:m1_join_blacklist_url params:@{@"to_uid":to_uid} success:^(id resDic, NSInteger code, NSString *message) {
        [M1Alert m1_alertWithTitle:nil message:@"已关进小黑屋"];
    } fail:^(id resDic, NSInteger code, NSString *message) {
        [M1Alert m1_alertWithTitle:nil message:message];
    } showAni:YES];
}
+(void)m1_removeImageCache{
    CGFloat folderSize = 0.0;
    
    //获取路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)firstObject];
    
    //获取所有文件的数组
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
    
    
    for(NSString *path in files) {
        
        NSString*filePath = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",path]];
        
        //累加
        folderSize += [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil].fileSize;
    }
    //转换为M为单位
    CGFloat sizeM = folderSize /1024.0/1024.0;
    
    if (sizeM >300) {
        for(NSString *p in files) {
            
            NSError*error;
            
            NSString*path = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",p]];
            
            if([[NSFileManager defaultManager]fileExistsAtPath:path])
            {
                BOOL isRemove = [[NSFileManager defaultManager]removeItemAtPath:path error:&error];
                
            }
        }
    }
}
@end
